import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import moment from "moment";
import * as request from "../../../../services/CommonHttpService";

export default {
  namespace: 'dataDetail',

  state: {
    ...baseState,
    deviceInfo: {},
    chartData: [],
    uploadStatistics: [],
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    // moment().format('YYYY-MM-DD HH:mm:ss'),

    * getDeviceInfo({payload}, {put, call}) {

      const {deviceId, errorMsg, notice, server} = payload;
      try {
        let result = yield call(request.commonGet, getBaseUrl() + "/one", {}, {deviceId}, server);
        result = {...result, lastTime: !!result.lastTime ? moment(result.lastTime).format('YYYY-MM-DD HH:mm:ss') : ""};

        yield put({
          type: 'initState',
          payload: {deviceInfo: result}
        });
      } catch (error) {
        console.error("baseGetPageInfo", error);
        yield put({type: 'loadError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
      }
    },

    * getStatistics({payload}, {put}) {

      const data = [];
      const size = 100;

      for (let i = 0; i < size; i++) {
        data.push({
          year: moment().subtract(size - i, 'days').format("YYYY-MM-DD"),
          value: Math.round(Math.random() * 30)
        });
      }

      yield put({
        type: 'initState',
        payload: {uploadStatistics: data}
      });
    }
  }

}

function getBaseUrl() {
  return "/device";
}

